// pages/report/report.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    types: [
      '色情',
      '广告',
      '重复',
      '欺诈',
      '其他'
    ],
    typeIndex: null,
    content: null,
    item: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let itemPage = app.pages.get('pages/item/item')
    this.setData({
      item: itemPage.data.obj
    })
  },

  typeMapping(value) {
    switch (value) {
      case '色情':
        return 'PORN';
      case '广告':
        return 'AD';
      case '重复':
        return 'REPEAT';
      case '欺诈':
        return 'FRAUD';
      case '其他':
        return 'OTHER';
    }
  },

  pickerChange: function(e) {
    this.setData({
      typeIndex: e.detail.value
    })
  },

  submit: function() {
    let types = this.data.types
    let index = this.data.typeIndex
    let content = this.data.content
    if (index == null || content == null) {
      wx.showToast({
        title: '不能存在空值',
        icon: 'none'
      })
      return;
    }

    let self = this
    let type = this.typeMapping(types[index])
    let host = app.globalData.host
    let token = wx.getStorageSync('token');

    let itemId = this.data.item.itemId
    let toId = this.data.item.userId


    wx.request({
      url: host + '/report',
      method: "POST",
      header: {
        'authorization': token,
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        type: type,
        content: content,
        itemId: itemId,
        toId: toId
      },
      success: function(res) {
        if (res.data.data) {
          wx.showToast({
            title: '提交成功',
            duration: 3000
          })
        }else{
          wx.showToast({
            title: '提交失败',
            icon: 'none',
            duration: 3000
          })
        }
      }

    })

  },

  changeData: function(e) {
    let name = e.currentTarget.dataset.name;
    this.setData({
      [name]: e.detail.value
    })
  },

})